package com.demo.aop.exception;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.ThrowsAdvice;

import java.lang.reflect.Method;

/**
 * 描述：
 *
 * @author lida
 * @time 2019/10/23 10:50
 */
public class ExceptionInterceptor implements ThrowsAdvice {
	public static Log logger = LogFactory.getLog("ExceptionLogger");
	/**
	 * 描述：拦截通用异常
	 * @param ex 通用异常
	 * @throws Throwable 异常标记
	 */
	public void afterThrowing(Exception ex) throws Throwable{
		StringBuilder message = new StringBuilder();
		message.append("*******捕获Exception异常******\n");
		message.append("异常类：").append(ex.getClass().getName()).append("\n");
		message.append("异常描述：").append(ex.getMessage()).append("\n");
		message.append("*************");
		logger.info(message);
	}

	/**
	 * 描述：拦截InterruptedException的方法
	 * @param m 拦截的方法
	 * @param args 参数列表
	 * @param target 目标对象
	 * @param ie 拦截的异常
	 * @throws Throwable 异常标记
	 */
	public void afterThrowing(Method m, Object[] args, Object target, InterruptedException ie) throws Throwable{
		StringBuilder message = new StringBuilder();
		message.append("*******捕获InterruptedException异常******\n");
		message.append("异常类：").append(ie.getClass().getName()).append("\n");
		message.append("异常类：").append(m.getName()).append("\n");
		message.append("异常描述：").append(ie.getMessage()).append("\n");
		message.append("*************");
		logger.info(message);
	}
}
